<?php

require_once($_SERVER['DOCUMENT_ROOT']."/apps/class/class.MysqliDatabase.php");

class transaction {
protected $id;
protected $description;
protected $value;
protected $category = array();
private $account;
protected $type;

//Format: "server=...; database=...; username=...; password=...; port=...; socket=..."
protected $db;


function __construct() 
    { 
      $this->db = new MysqliDatabase("server=localhost; database=aikanaro; username=aikanaro; password=aikanaro");
    }

    public function set_transaction_mandatory($description,$value,$account,$type){
      $this->id = uniqid('', true);
      $this->description = $description;
      $this->value = $value;
      $this->account = $account;
      $this->type = $type;
      if($this->type == 0){
        $this->value = 0 - $value;
      }else{
        $this->value = $value;
      }
    }
    
    public function set_category($categories){
      $this->category = $categories;
    }
     
    public function commit_transaction(){
      $sql = "INSERT INTO `aikanaro`.`transaction` (`id`, `description`, `value`, `type`) VALUES ('".$this->id."','".$this->description."','".$this->value."','".$this->type."');";
      $queries[] = $sql;
      $sql = "INSERT INTO `aikanaro`.`transaction_account` (`transaction_id`, `account_id`) VALUES ('".$this->id."','".$this->account."');";
      $queries[] = $sql;
//      foreach($this->category as $category){
//        $sql = "INSERT INTO `aikanaro`.`transaction_categories` (`transaction_id`, `category_id`) VALUES (\'".$this->id."', \'".$category."');";
//        $queries[] = $sql;
//      }
      $result = $this->db->transaction_query($queries);
    }
}


class account {
  private $account_id;
  private $account;
  private $user;
  private $curr;
  protected $db;
  
  function __construct($account_id){
    $this->db = new MysqliDatabase("server=localhost; database=aikanaro; username=aikanaro; password=aikanaro");
    $this->user = new user();
    $this->user->get_user_from_db($_SESSION['user']);
    $this->account_id = $account_id;
    $this->get_account_from_db();    
  }
  
  public function get_currency(){
    return $this->curr;
  }
    
  public function create_account_in_db($description,$currency){
    $account_id = uniqid('', true);
    $db = new MysqliDatabase("server=localhost; database=aikanaro; username=aikanaro; password=aikanaro");
    $user = new user();
    $user->get_user_from_db($_SESSION['user']);
    $query = "INSERT INTO `account` (`id`, `description`, `currency`, `user`) VALUES ('".$account_id."','".$description."','".$currency."','".$user->get_user_id()."');";
    $result = $db->query($query);
    return $account_id;  
  }
  public function get_account_from_db(){
    $query = "SELECT * from `account` where id = '".$this->account_id."';";
    $result = $this->db->query_one($query);
    if(!isset($result)){
      throw new appException("The account does not exist");  
    }
    $this->account = $result->description;
    $this->curr = $result->curr;
    $this->account_id = $result->id;
    $this->user_id = $result->user;
  }
}

class accounts{
  private $accounts;
  private $user;
  private $format;
  protected $db;
  
  function __construct($format = "object"){
    $this->format = $format;
    $this->db = new MysqliDatabase("server=localhost; database=aikanaro; username=aikanaro; password=aikanaro");
    $this->user = new user();
    $this->user->get_user_from_db($_SESSION['user']);
    $query = "SELECT * from `account` where user = '".$this->user->get_user_id()."';";
    $this->accounts = $this->db->query_all($query);
  }
  
  function get_list_accounts(){
    if($this->format == 'json'){
    return json_encode($this->accounts);
    } else{
    return $this->accounts;
    } 
  }

}

class user {
  private $user_id;
  private $user;
  protected $db;
  
  function __construct(){
    $this->db = new MysqliDatabase("server=localhost; database=aikanaro; username=aikanaro; password=aikanaro");      
  }
  
  public function get_user_id(){
    return $this->user_id;
  }
  
    
  public function create_user_in_db($username,$userpass){
    $this->user_id = uniqid('', true);
    $this->user = $username;
    $query = "INSERT INTO `user` (`id`, `user`,`pass`) VALUES ('".$this->user_id."','".$username."','".md5($userpass)."');";
    $db = new MysqliDatabase("server=localhost; database=aikanaro; username=aikanaro; password=aikanaro");
    $result = $db->query($query);  
  }
  
  public function login_user($username,$userpass){
    $query = "select id from `user` where user = '".$username."' and pass = '".md5($userpass)."';";
    $db = new MysqliDatabase("server=localhost; database=aikanaro; username=aikanaro; password=aikanaro");
    $result = $db->query_one($query);
    if( !isset($result) ){
      throw appException("User name or passoword not valid");    }
    $this->user_id = $result->id;
    $this->get_user_from_db($this->user_id);  
  }
  
  
  public function get_user_from_db($user_id){
    $query = "SELECT * from `user` where id = '".$user_id."';";
    $result = $this->db->query_one($query);     
    $this->user = $result->user;
    $this->user_id = $user_id;
  }
}

class appException extends Exception {
    private $variable = array();
    
    public function __construct($message, $variable = null, $code = 0, Exception $previous = null) {
        if(isset($variable)){
          $this->variable = $variable;
        }
        parent::__construct($message, $code, $previous);
    }
    
    public function get_arguments(){
      return $this->variable;
    }
    

}



class categories{
  private $categories;
  private $user;
  private $format;
  protected $db;
  
  function __construct($format = "object"){
    $this->format = $format;
    $this->db = new MysqliDatabase("server=localhost; database=aikanaro; username=aikanaro; password=aikanaro");
    $this->user = new user();
    $this->user->get_user_from_db($_SESSION['user']);
    $query = "SELECT * from `categories` where user = '".$this->user->get_user_id()."';";
    $this->categories = $this->db->query_all($query);
  }
  
  function get_list_accounts(){
    if($this->format == 'json'){
    return json_encode($this->categories);
    } else{
    return $this->categories;
    } 
  }
  
  function search_categories_in_db($string,$format){
    $db = new MysqliDatabase("server=localhost; database=aikanaro; username=aikanaro; password=aikanaro");
    $user = new user();
    $user->get_user_from_db($_SESSION['user']);
    $query = "SELECT id , description FROM `categories` inner join categories_user on categories.id = categories_user.category_id where categories_user.user_id = '".$user->get_user_id()."'";
    $categories = $db->query_all($query);
    if($format == 'json'){
    return json_encode($categories);
    } else{
    return $categories;
    }   
  }

}

class category {
  private $category_id;
  private $category;
  private $user;
  private $curr;
  protected $db;
  
  function __construct($category_id){
    $this->db = new MysqliDatabase("server=localhost; database=aikanaro; username=aikanaro; password=aikanaro");
    $this->user = new user();
    $this->user->get_user_from_db($_SESSION['user']);
    $this->category_id = $category_id;
    $this->get_category_from_db();    
  }
    
  public function create_category_in_db($description){
    $category_id = uniqid('', true);
    $db = new MysqliDatabase("server=localhost; database=aikanaro; username=aikanaro; password=aikanaro");
    $user = new user();
    $user->get_user_from_db($_SESSION['user']);
    $query = "INSERT INTO `categories` (`id`, `description`) VALUES ('".$category_id."','".$description."');";
    $result = $db->query($query);
    $query = "INSERT INTO `categories_user` (`category_id`, `user_id`) VALUES ('".$category_id."','".$user->get_user_id()."');";
    $result = $db->query($query);
    return $category_id;  
  }
  public function get_account_from_db(){
    $query = "SELECT * from `account` where id = '".$this->account_id."';";
    $result = $this->db->query_one($query);
    if(!isset($result)){
      throw new appException("The account does not exist");  
    }
    $this->account = $result->description;
    $this->curr = $result->curr;
    $this->account_id = $result->id;
    $this->user_id = $result->user;
  }
}









?>